package com.ywk.base.toolClass.collections;

import com.ywk.base.javaBase.collection.set.Student;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CollectionsDemo {
    public static void main(String[] args) {
        List<Student> res=new ArrayList<>();
        //创建元素对象
        Student s1=new Student("zhangsan",20);
        Student s2=new Student("lis",22);
        Student s3=new Student("wangwu",24);
        Student s4=new Student("chenliu",26);
        Student s5=new Student("zhangsan",22);
        Student s6=new Student("qianqi",24);
        //将元素对象添加到集合对象中
        res.add(s1);
        res.add(s2);
        res.add(s3);
        res.add(s4);
        res.add(s5);
        res.add(s6);

        sort(res);
        for(Student s:res){
            System.out.println(s.getName()+"-----------"+s.getAge());
        }
    }

    private static void sort(List<Student> reList){
        Collections.sort(reList, new Comparator<Student>() {
            @Override
            public int compare(Student m1, Student m2) {
                // 升序
                if ( Long.valueOf(m1.getAge())>Long.valueOf(m2.getAge())) {
                    return 1;
                }
                //降序
                /*if ( Long.valueOf(m1.getAge())<Long.valueOf(m2.getAge())) {
                    return 1;
                }*/
                if (Long.valueOf(m1.getAge()) == Long.valueOf(m2.getAge())) {
                    return 0;
                }
                return -1;
            }
        });
    }
}
